package UILayer;
import CtrLayer.*;

/**
 * Submenu "friends".
 * Here the loaner can add, delete or find friends.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonUI
{
    // instance variables
    private PersonCtr pCtr;
    private Parser parser;

    public  PersonUI(PersonCtr pCtr)
    {
        // initialise instance variables
        this.parser = new Parser();
        this.pCtr = pCtr;
    }

    public void personMenu()
    {
        boolean exit = false;
        writeMenu();
        while(!exit)
        {
            int choice = parser.scanChoiceInput();
            if(choice == 1) { //Create friend
                createPerson();
                writeMenu();
            }
            if(choice == 2) { //delete friend
                deletePerson();
                writeMenu();
            }
            if(choice == 3) { //find friend
                findPerson();
                writeMenu();;
            }
            if(choice == 4) { //update information
                updatePerson();
                writeMenu();;
            }
            if(choice == 5) { //go back
                exit = true;
            }
        }
    }

    /**
     * Writes the menu for person
     */
    public void writeMenu()
    {
        System.out.println("***** Friend menu *****");
        System.out.println("*  (1) add new person *");
        System.out.println("*  (2) delete person  *");
        System.out.println("*  (3) find person    *");
        System.out.println("*  (4) update person  *");        
        System.out.println("*  (5) Back           *");
        System.out.println("***********************");
    }

    public void createPerson()
    {
        System.out.println("Enter friends name: ");
        String name = parser.scanInputString();

        System.out.println("Address: ");
        String address = parser.scanInputString();

        System.out.println("Postal code: ");
        String postalCode = parser.scanInputString();

        System.out.println("City: ");
        String city = parser.scanInputString();

        System.out.println("Phone number: ");
        String phone = parser.scanInputString();       

        pCtr.createPerson(name, address, postalCode, city, phone);
    }

    public void deletePerson()
    {
        System.out.println("Enter phone number of person to delete: ");     
        pCtr.deletePerson(pCtr.findPerson(parser.scanInputString()));
        System.out.println("Person  was deleted\n");
    }

    public void findPerson()
    {
        System.out.println("Enter friends phone number: ");
        String phone = parser.scanInputString();

        if(pCtr.findPerson(phone) != null) {
            pCtr.findPerson(phone).printInfo();
        }
        else {
            System.out.println("Could not find a person with phone number: "+phone+"\n");
        }
    }

    public void updatePerson()
    {
        System.out.println("Enter phone number of the persons who you wish to update: ");
        String currPhone = parser.scanInputString();

        System.out.println("Enter new name: ");
        String name = parser.scanInputString();

        System.out.println("Enter new address: ");
        String address = parser.scanInputString();

        System.out.println("Enter new postalCode: ");
        String postalCode= parser.scanInputString();

        System.out.println("Enter new city: ");
        String city = parser.scanInputString();

        System.out.println("Enter new phone number: ");
        String phone = parser.scanInputString();

        pCtr.updatePerson(currPhone, name, address, postalCode, city, phone);
    }
}
